package com.blockchain.securewallet.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.manager.ThreadPoolManager;
import com.blockchain.securewallet.utils.StringUtil;
import com.blockchain.securewallet.utils.UIHelper;

import java.lang.ref.WeakReference;

import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import razerdp.basepopup.BasePopupWindow;

/**
 * Created by Lopez on 2018/6/22.
 */
public class PopupSeedQRCode extends BasePopupWindow implements View.OnClickListener {

    private Activity context;
    private ImageView iv_qr_code;
    private static String seedWords;

    public PopupSeedQRCode(Context context, String words) {
        super(context);
        //全屏
        setPopupWindowFullScreen(true);
        TextView tv_ok = (TextView) findViewById(R.id.tv_ok);
        iv_qr_code = (ImageView) findViewById(R.id.iv_qr_code);

        this.context = (Activity) context;
        setViewClickListener(this, tv_ok);

        seedWords = words;
        createQRCode();
    }

    @Override
    protected Animation initShowAnimation() {
        int height = UIHelper.getWidthOrHeight(false);
        return getTranslateVerticalAnimation(-height, 0, 300);
    }

    @Override
    protected Animation initExitAnimation() {
        //return getDefaultAlphaAnimation(false);
        int height = UIHelper.getWidthOrHeight(false);
        return getTranslateVerticalAnimation(0, -height, 200);
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.popup_seed_qrcode);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.cl_qr_code);
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    private void createQRCode() {
        ThreadPoolManager.executeOnExecutor(new Task(context, iv_qr_code));
    }

    static class Task extends AsyncTask<Void, Void, Bitmap> {

        WeakReference<Activity> weakReference;
        WeakReference<ImageView> ivWeakReference;


        Task(Activity activity, ImageView iv_qr_code) {
            this.weakReference = new WeakReference<>(activity);
            this.ivWeakReference = new WeakReference<>(iv_qr_code);
        }


        @Override
        protected Bitmap doInBackground(Void... voids) {
            return QRCodeEncoder.syncEncodeQRCode(seedWords, BGAQRCodeUtil.dp2px(weakReference.get(), 150));
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap != null) {
                ivWeakReference.get().setImageBitmap(bitmap);
                ivWeakReference.get().setVisibility(View.VISIBLE);
            } else {
                Toast.makeText(weakReference.get(), StringUtil.getResourceString(R.string.toast_qr_coed_fail), Toast.LENGTH_SHORT).show();
            }
        }
    }

}
